package Datos;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.CallableStatement;
import java.util.*;

public class LineaLiquidacion {
	Sesion oSesion;
	
	public LineaLiquidacion() {};

	public int Add(int idLiquidacion, int idDetalle, Entidades.LineaLiquidacion item) throws Exception{
		try{
		Connection oCnn = Datos.General.CrearConexion();
		String storedProcedure = "{call cpd.LineaLiquidacionAdd(?,?,?,?)}";
		CallableStatement cs =oCnn.prepareCall(storedProcedure);
		poneDatos(idLiquidacion, idDetalle, item, cs);
		boolean pipi = cs.execute();
		int numero = 0;
		if (pipi) numero = 1;
		return numero;
		
		} catch (Exception ex){
			   System.out.println("SQLException: " + ex.getMessage());
			   return -1;
		}
	}
	public void Update(int idLiquidacion, int idDetalle, Entidades.LineaLiquidacion item) throws Exception{
		try{
			Connection oCnn = Datos.General.CrearConexion();
			String storedProcedure = "{call cpd.LineaLiquidacionUpdate(?,?,?,?)}";
			CallableStatement cs =oCnn.prepareCall(storedProcedure);
			//poneDatos(item,cs);
			boolean pipi = cs.execute();
			return;
		} catch (Exception ex){
			   System.out.println("SQLException: " + ex.getMessage());
		}
	}
	public void Delete(int id) throws Exception{
		try{
			Connection oCnn = Datos.General.CrearConexion();
			String storedProcedure = "{call cpd.LineaLiquidacionDelete(?)}";
			CallableStatement cs =oCnn.prepareCall(storedProcedure);
			cs.setInt("mIdLineaLiquidacion", id);

			boolean pipi = cs.execute();
			return;
		} catch (Exception ex){
			   System.out.println("SQLException: " + ex.getMessage());
		}
	}
	public List GetOne(int id) throws Exception{
		Statement stmt = null;   //
		ResultSet rs = null;	//
		Connection oCnn = Datos.General.CrearConexion();
		String storedProcedure = "{call cpd.LineaLiquidacionGetOne(?)}";
		CallableStatement cs = oCnn.prepareCall(storedProcedure);
		cs.setInt("mIdLineaLiquidacion", id);
		rs = cs.executeQuery();
		return sacaDatos(rs);
	}
	public List GetAll() throws Exception{
		Statement stmt = null;   //
		ResultSet rs = null;	//
		Connection oCnn = Datos.General.CrearConexion();
		String storedProcedure = "{call cpd.LineaLiquidacionGetAll()}";
		CallableStatement cs = oCnn.prepareCall(storedProcedure);
		rs = cs.executeQuery();
		return sacaDatos(rs);
	}
	public void Abrir() throws Exception{
		oSesion = new Sesion();
		try{
			String sql = "HANDLER `linealiquidacion` OPEN";
			oSesion.getSesion().executeQuery(sql);
		} catch (Exception ex){
			System.out.println("SQLException: " + ex.getMessage());
		}
	}
	public void Cerrar() throws Exception{
		try{
			String sql = "HANDLER `linealiquidacion` CLOSE";
			oSesion.getSesion().executeQuery(sql);
		} catch (Exception ex){
			System.out.println("SQLException: " + ex.getMessage());
		}
	}

	public List Consulta(String consulta) throws Exception{
		oSesion = new Sesion();
		ResultSet rs = null;	//
		String sql = "SELECT * FROM linealiquidacion " + consulta + ";";
		rs = oSesion.getSesion().executeQuery(sql);
		return sacaDatos(rs);
	}
	private List sacaDatos(ResultSet rs){
		List oDatos = new ArrayList();
		Entidades.LineaLiquidacion oLineaLiquidacion = null;
		Float tasa = null;
		int   cpte = 0;
		try{
			while(rs.next()){
				//creo el objeto CPD del DetalleOrdenVenta
				List oCPDs = new ArrayList();
				Entidades.CPD oCPD = null;
				oCPDs = new Datos.CPD().GetOne(rs.getInt(1));
				while(!oCPDs.isEmpty()){
					oCPD = (Entidades.CPD) oCPDs.remove(0);
				}
				tasa = rs.getFloat(3);
				cpte = rs.getInt(4);
				oLineaLiquidacion = new Entidades.LineaLiquidacion(oCPD, tasa, cpte);
				oDatos.add(oLineaLiquidacion);
				oLineaLiquidacion = null;
				tasa = null;
				cpte = 0;
				//oDetalleOrdenVenta = new Entidades.DetalleOrdenVenta(oCPD);
				//oDatos.add(oDetalleOrdenVenta);
				//oDetalleOrdenVenta = null;
			}
		} catch (Exception ex){
			System.out.println("SQLException: " + ex.getMessage());
		}
		return oDatos;
	}
	private void poneDatos(int idLiquidacion, int idDetalle, Entidades.LineaLiquidacion item, CallableStatement cs){
		try{
			cs.setInt("mIdLiquidacion", idLiquidacion);
			//cs.setInt("mIdDetalle", idDetalle);
			cs.setInt("mIdCPD",(int) item.getCPD().getIdCPD());
			cs.setFloat("mTasaNegociada", (Float) item.getTasaNegociada() );
			cs.setInt("mIdContraparte", (int) item.getIdContraparte());
		} catch (Exception ex){
			   System.out.println("SQLException: " + ex.getMessage());
		}
	}

}
